import requests
import re
import pandas as pd
import time
import datetime
 
url = 'http://datacenter-web.eastmoney.com/api/data/v1/get?'
 
name_list = []
code_list = []
trader_date_list = []
close_list = []
change_rate_list = []
buy_num_list = []
result_list = []
 
result_df = pd.DataFrame()
for page in range(1, 4):
    params = (
        ('callback', 'jQuery112305930880286224138_1632364981303'),
        ('sortColumns', 'NET_BUY_AMT,TRADE_DATE,SECURITY_CODE'),
        ('sortTypes', '-1,-1,1'),
        ('pageSize', '50'),
        ('pageNumber', str(page)),
        ('reportName', 'RPT_ORGANIZATION_TRADE_DETAILS'),
        ('columns', 'ALL'),
        ('source', 'WEB'),
        ('clientl', 'WE'),
        ('filter', "(TRADE_DATE>='2021-09-17')")
    )
    response = requests.get(url, params=params)
    text = response.text
    print(text)
    # re准则查找数据
    name = re.findall('"SECURITY_NAME_ABBR":"(.*?)"', text)     # 名称
    code = re.findall('"SECURITY_CODE":"(.*?)"', text)      # 股票代码
    trader_date = re.findall('"TRADE_DATE":"(.*?)"', text)  # 交易日期
 
    close = re.findall('"CLOSE_PRICE":(.*?)\,', text)   # 收盘价
    change_rate = re.findall('"CHANGE_RATE":(.*?)\,', text)   # 涨幅
    buy_num = re.findall('"BUY_TIMES":(.*?)\,', text)   # 买入机构数量
 
    # 将对应的列表里的数据全部加起来
    name_list = name_list + name
    code_list = code_list + code
    trader_date_list = trader_date_list + trader_date
    close_list = close_list + close
    change_rate_list = change_rate_list + change_rate
    buy_num_list = buy_num_list + buy_num
 
    time.sleep(2)
# 将所有列表合并成二维数组
result_list = [trader_date_list, code_list, name_list, close_list, change_rate_list, buy_num_list]
# 将数据转为DataFrame格式
result_df = pd.DataFrame(result_list).T.rename(
    columns={0: '交易日期', 1: '股票代码', 2: '股票名称', 3: '收盘价', 4: '涨幅', 5: '买入机构'})
result_df['交易日期'] =pd.to_datetime(result_df['交易日期'])  # 时间只取年月日
result_df = result_df.sort_values(by='交易日期', ascending=True)
print(result_df)